﻿/*
 * 竞价设置
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model;
using System.Drawing;

public partial class Manage_CompetePriceSetting : System.Web.UI.Page, IAccess
{
    protected string trdisplay = "inline";
    protected void Page_Load(object sender, EventArgs e)
    {
        CurrentAccount.CheckSessionTimeOut(this);
        if (!IsPostBack)
        {
            BindData();
        }
    }
    protected void btnSet_Click(object sender, EventArgs e)
    {

        int times = int.Parse(txtcompetetimes.Text);
        // 轮次一样 暂时不变
        var current = BLL_Factory.IBLL_Manage.GetCompeteTimeConfigByProjectid(CurrentAccount.ProjectId);
        if (times == current.Count)
        {
            return;
        }
        var list = new List<CompeteTimeConfig>();
        for (int i = 0; i < times; i++)
        {
            if (i < current.Count)
            {
                list.Add(current[i]);
            }
            else
                list.Add(new CompeteTimeConfig() { ProjectId = CurrentAccount.ProjectId, CompeteTime = i + 1 });
        }
        BLL_Factory.IBLL_Manage.SetCompeteTimeConfig(list);
        BindData();
    }
    protected void btnSetTime_Click(object sender, EventArgs e)
    {
        var list = new List<CompeteTimeConfig>();
        foreach (RepeaterItem item in rptimes.Items)
        {


            var i = new CompeteTimeConfig()
            {
                CompeteStartTime = new HZ.Common.ConvertHelper((item.FindControl("txtCompeteBegin") as TextBox).Text).ToDateTime,
                CompeteEndTime = new HZ.Common.ConvertHelper((item.FindControl("txtCompeteEnd") as TextBox).Text).ToDateTime,

                ProjectId = CurrentAccount.ProjectId,
                CompeteTime = item.ItemIndex + 1
            };

            if (i.CompeteEndTime < i.CompeteStartTime)
            {
                HZ.Web.MessageBox.Show("截止时间不应早于起始时间");
            }
            list.Add(i);
        }
        BLL_Factory.IBLL_Manage.SetCompeteTimeConfig(list);
        HZ.Web.WebUtil.Alert("设置成功", this);
    }

    private void BindData()
    {
        var pc = BLL_Factory.IBLL_Manage.GetProjectConfig(CurrentAccount.ProjectId);
        ViewState["CompeteSwitch"] = pc.CompeteSwitch ?? false;
        if (pc.CompeteSwitch ?? false)
        {
            lblcompetestatus.ForeColor = Color.Green;
            lblcompetestatus.Text = "竞价开关目前状态：开启";
            btncompeteswitch.Text = "关闭竞价开关";
        }
        else
        {

            lblcompetestatus.ForeColor = Color.Red;
            lblcompetestatus.Text = "竞价开关目前状态：关闭";
            btncompeteswitch.Text = "开启竞价开关";

        }

        var list = BLL_Factory.IBLL_Manage.GetCompeteTimeConfigByProjectid(CurrentAccount.ProjectId);
        if (list.Count == 0)
        {
            trdisplay = "none";
        }
        rptimes.DataSource = list;
        rptimes.DataBind();
    }


    protected void btncompeteswitch_Click(object sender, EventArgs e)
    {
        bool sw = (bool)ViewState["CompeteSwitch"];
        var pc = new ProjectConfig() { ProjectId = CurrentAccount.ProjectId };
        if (sw)
        {
            pc.CompeteSwitch = false;

        }
        else
        {
            pc.CompeteSwitch = true;
        }

        BLL_Factory.IBLL_Manage.SetProjectConfig(pc);
        BindData();
    }
    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Manage);
            return list;
        }

    }

    #endregion
}
